package edu.uoc.tdp.pac3.client;

import javax.swing.*;
import java.rmi.RemoteException;
import java.util.Date;
import edu.uoc.tdp.pac3.common.tools.*;
import edu.uoc.tdp.pac3.common.GestorIncidenciesInterface;

/**
 * Clase Pnatalla d'Alta d'anotacions a una incidència
 * 
 * @author Eloi Rebollo i Francesc Cabrera
 * @version 1.0
 */
public class PantallaActuacions extends JPanel {

    private GestorIncidenciesInterface objServeis = null;
    private TDSLanguageUtils mess = null;
    private JLabel lblTitle = null;
    private JLabel lblIncidencia = null;
    private JLabel lblUsers = null;
    private JLabel lblResol = null;
    private JTextArea txtArea = null;
    private JButton btnSave = null;
    private JComboBox cmbUsers = null;
    private JComboBox cmbIncidencia = null;
    private JComboBox cmbSolved = null;

    /**
     * @param args
     */
    public static void main(String[] args) {
    }

    /**
     * This is the default constructor
     */
    public PantallaActuacions() {
        super();
        initialize();
    }

    public PantallaActuacions(GestorIncidenciesInterface pObjServeis) throws RemoteException {
        super();
        objServeis = pObjServeis;
        initialize();
    }

    /**
     * This method initializes this
     * 
     * @return void
     */
    private void initialize() {
        lblTitle = new JLabel();
        lblTitle.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        lblTitle.setBounds(20, 25, 300, 20);
        lblTitle.setText(mess.getMessage("action.title"));

        lblIncidencia = new JLabel();
        lblIncidencia.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblIncidencia.setBounds(20, 60, 250, 20);
        lblIncidencia.setText(mess.getMessage("action.combo.usuaris"));

        lblUsers = new JLabel();
        lblUsers.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblUsers.setBounds(20, 90, 250, 20);
        lblUsers.setText(mess.getMessage("action.combo.incidencia"));

        lblResol = new JLabel();
        lblResol.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        lblResol.setBounds(20, 120, 250, 20);
        lblResol.setText(mess.getMessage("action.combo.resol"));

        txtArea = new JTextArea();
        txtArea.setBorder(javax.swing.BorderFactory.createTitledBorder(mess.getMessage("action.area.title")));
        txtArea.setBounds(10, 150, 655, 160);

        this.setLayout(null);
        this.setBounds(0, 0, 800, 350);
        this.add(lblTitle, null);
        this.add(lblIncidencia, null);
        this.add(getCmbIncidencia(), null);

        this.add(lblUsers, null);
        this.add(getCmbUser(), null);

        this.add(lblResol, null);
        this.add(getCmbSolved(), null);
        this.add(txtArea);

        this.add(getBtnSave(), null);

    }

    private JComboBox getCmbIncidencia() {
        if (cmbIncidencia == null) {
            try {
                cmbIncidencia = new JComboBox(objServeis.vctIncidencia());
                cmbIncidencia.setBounds(300, 60, 370, 25);
            } catch (RemoteException e) {
                JOptionPane.showMessageDialog(null,
                        mess.getMessage("action.err.rmi") + "\n" + e.getMessage(),
                        mess.getMessage("action.err"), JOptionPane.ERROR_MESSAGE);
            }
        }
        return cmbIncidencia;
    }
    
    /**
     * Inicialitza el contingut del cmbSolved d'usuaris	
     * 	
     * @return javax.swing.JComboBox	
     */
    private JComboBox getCmbUser() {
        if (cmbUsers == null) {
            try {
                cmbUsers = new JComboBox(objServeis.vctUsers());
                cmbUsers.setBounds(300, 90, 370, 25);
            } catch (RemoteException e) {
                JOptionPane.showMessageDialog(null,
                        mess.getMessage("action.err.rmi") + "\n" + e.getMessage(),
                        mess.getMessage("action.err"), JOptionPane.ERROR_MESSAGE);
            }
        }
        return cmbUsers;
    }

    private JComboBox getCmbSolved() {
        
        cmbSolved = new JComboBox();
        ComboItem cmbItem = new ComboItem(0, mess.getMessage("action.combo.resol.no"));
        cmbSolved.addItem(cmbItem);
        cmbItem = new ComboItem(1, mess.getMessage("action.combo.resol.yes"));
        cmbSolved.addItem(cmbItem);
        cmbSolved.setBounds(300, 120, 370, 25);
        
        return cmbSolved;
    }

    /**
     * This method initializes getBtnSave
     * 	
     * @return javax.swing.JButton	
     */
    private JButton getBtnSave() {
        if (btnSave == null) {
            btnSave = new JButton();
            btnSave.setBounds(10, 320, 660, 30);
            btnSave.setText(mess.getMessage("action.button.save"));
            btnSave.addActionListener(new java.awt.event.ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent e) {
                    if (Guardar() == -1) {
                        JOptionPane.showMessageDialog(null,
                                mess.getMessage("action.action.services.ko"),
                                mess.getMessage("action.action.services"), JOptionPane.WARNING_MESSAGE);
                    } else {
                        JOptionPane.showMessageDialog(null,
                                mess.getMessage("action.action.services.ko"),
                                mess.getMessage("action.action.services"), JOptionPane.WARNING_MESSAGE);
                    }
                }
            });
        }
        return btnSave;
    }

    protected Integer Guardar() {

        Integer ret = -1;
        boolean sol = false;

        ComboItem Request = (ComboItem) cmbIncidencia.getSelectedItem();
        ComboItem User = (ComboItem) cmbUsers.getSelectedItem();
        ComboItem Solved = (ComboItem) cmbSolved.getSelectedItem();
        
        if (Solved.getID() == 1) {
            sol = true;
        }
        try {
            ret = objServeis.saveNote(Request.getID(), User.getID(), new Date(), txtArea.getText());
            try {
                ret = objServeis.updateRequest(Request.getID(), sol);
            } catch (Exception e1) {
                JOptionPane.showMessageDialog(null,
                        mess.getMessage("altanova.action.services.ko") + "\n" + e1.getMessage(),
                        mess.getMessage("altanova.title"), JOptionPane.ERROR_MESSAGE);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,
                    mess.getMessage("modifica.action.services.ko") + "\n" + e.getMessage(),/*FALLA*/
                    mess.getMessage("gestor.note.err"), JOptionPane.ERROR_MESSAGE);
        }
        return ret;
    }
}
